﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class RP02SUMReport : BaseReport
    {
        ArrayList listSumByFactory;
        string strCompany = "HLT";
        string strExcelFile = "";
        public RP02SUMReport()
            : base()
        {
        }
        public RP02SUMReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            columns.Add("VKPAUFNUM");
            columns.Add("KDKUNDNA");
            columns.Add("VKPWUTERM");
            columns.Add("STDBATCHNO");
            columns.Add("ARTICLE");
            columns.Add("SBEAMLENG");
            columns.Add("SBEAMNO");
            columns.Add("WEFTYARNSTATUS");
        }
        public void FillInExcelData(ArrayList columns, DataTable dtSoruce)
        {
            try
            {
                exAgent.SetCellValue(2, 3, DateTime.Now.Year.ToString()
                    + "年" + DateTime.Now.Month.ToString()
                    + "月" + DateTime.Now.Day.ToString()
                    + "日");
                listSumByFactory = new ArrayList();
                WVFactory wvFactory = null;
                DataTable tableWBOrder = dsSource.Tables["WBOrder"];
                int x = 31, y = 1;
                string VKPAUFNUM;
                string KDKUNDNA;
                string VKPWUTERM;
                string STDBATCHNO;
                string ARTICLE;
                string SBEAMLENG;
                string SBEAMNO;
                string WEFTYARNSTATUS;                
                string WBFactoryName;
                string PBFactoryName;
                string WBOrder;
                int PBRunning;
                int PBFinished;
                int PBWaiting;
                int PBPlan;
                foreach (DataRow dwWBOrder in tableWBOrder.Rows)
                {
                    y = 1;
                    VKPAUFNUM = dwWBOrder["VKPAUFNUM"].ToString();
                    KDKUNDNA = dwWBOrder["KDKUNDNA"].ToString();
                    VKPWUTERM = dwWBOrder["VKPWUTERM"].ToString();
                    STDBATCHNO = dwWBOrder["STDBATCHNO"].ToString();
                    ARTICLE = dwWBOrder["ARTICLE"].ToString();
                    SBEAMLENG = dwWBOrder["SBEAMLENG"].ToString();
                    SBEAMNO = dwWBOrder["SBEAMNO"].ToString();
                    WEFTYARNSTATUS = dwWBOrder["WEFTYARNSTATUS"].ToString();
                    WBOrder = dwWBOrder["WBOrder"].ToString().Trim();
                    
                    WBFactoryName = dwWBOrder["WBFactoryName"].ToString();
                    PBFactoryName = dwWBOrder["PBFactoryName"].ToString();
                    
                    PBRunning = System.Convert.ToInt16(dwWBOrder["PBRunning"].ToString());
                    PBFinished = System.Convert.ToInt16(dwWBOrder["PBFinished"].ToString());
                    PBWaiting = System.Convert.ToInt16(dwWBOrder["PBWaiting"].ToString());
                    PBPlan = System.Convert.ToInt16(dwWBOrder["PBPlan"].ToString());
                    exAgent.SetCellValue(x, y, VKPAUFNUM); y++;
                    exAgent.SetCellValue(x, y, KDKUNDNA); y++;
                    exAgent.SetCellValue(x, y, VKPWUTERM); y++;
                    exAgent.SetCellValue(x, y, STDBATCHNO); y++;
                    exAgent.SetCellValue(x, y, ARTICLE); y++;
                    exAgent.SetCellValue(x, y, SBEAMLENG); y++;
                    exAgent.SetCellValue(x, y, SBEAMNO); y++;
                    exAgent.SetCellValue(x, y, WEFTYARNSTATUS); y++;
                    exAgent.SetCellValue(x, y, WBOrder); y++;
                    exAgent.SetCellValue(x, y, WBFactoryName); y++;
                    exAgent.SetCellValue(x, y, PBFactoryName); y++;
                    exAgent.SetCellValue(x, y, PBRunning); y++;
                    exAgent.SetCellValue(x, y, PBWaiting); y++;
                    exAgent.SetCellValue(x, y, PBFinished); y++;
                    exAgent.SetCellValue(x, y, PBPlan); y++;
                    var query2 = from WVFactory wvfactory in listSumByFactory
                                 where wvfactory.PBFactoryName == PBFactoryName
                                 select wvfactory;
                    foreach (WVFactory wvfactory in query2)
                    {
                        wvFactory = wvfactory;
                    }
                    if (wvFactory == null)
                    {
                        wvFactory = new WVFactory("", PBFactoryName);
                        wvFactory.PBFactoryName = PBFactoryName;
                        listSumByFactory.Add(wvFactory);
                    }
                    wvFactory.PBFinished += PBFinished;
                    wvFactory.PBRunning += PBRunning;
                    wvFactory.PBWaiting += PBWaiting;
                    wvFactory.PBPlan += PBPlan; 
                    x++;
                }
                x = 4;

                foreach (WVFactory wvXFactory in listSumByFactory)
                {
                    y = 10;
                    exAgent.SetCellValue(x, y, wvXFactory.PBFactoryName); y++;
                    exAgent.SetCellValue(x, y, wvXFactory.PBRunning); y++;
                    exAgent.SetCellValue(x, y, wvXFactory.PBWaiting); y++;
                    exAgent.SetCellValue(x, y, wvXFactory.PBFinished); y++;
                    exAgent.SetCellValue(x, y, wvXFactory.PBPlan); y++;
                    x++;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void HideNullRows()
        {
            try
            {
                int HidedRows = 0;
                int rowstart = 1;
                int rowend = 1;
                int col1, col2, col3;

                var val1 = exAgent.GetCellValue(1, 1);

                rowstart = 4; rowend = 29; col1 = 10; col2 = 13;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, col1, col2).Trim();
                    if ((string)val1 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col2);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void WriteExcelReport(string fileType)
        {
            Queue CellQueue = new Queue();

            string filename = "";
            string sheetName = "";
            string Standard = "RP02SUM";
            strExcelFile = @"HLT\HLTPlanningReport.xls";
            string filePath = @"C:\intex.w\Report\HLT\HLTPlanningReport.xls";
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"Planning\织造\WB" + filedate + ".xls";
            sheetName = DateTime.Now.ToShortTimeString().Replace(@":", "");
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData(columns, dsSource.Tables["ABPOS"]);
            HideNullRows();
            base.WriteExcelReport(fileType);
        }
    }
}
